import 'dart:convert';

import 'package:beautiful_demo/common/beans/ws_bean.dart';
import 'package:beautiful_demo/common/manager/user.dart';
import 'package:beautiful_demo/common/manager/websocket.dart';
import 'package:beautiful_demo/mvc/chat/friend/controller.dart';
import 'package:beautiful_demo/mvc/chat/room/controller.dart';
import 'package:beautiful_demo/mvc/controller.dart';
import 'package:intl/intl.dart';
import 'package:vibrate/vibrate.dart';

import 'chat_list_bean.dart';
import 'view.dart';
import 'package:flutter_mvc/flutter_mvc.dart';

///Description:聊天列表
///Author:teihideharu
class ChatListController extends BaseController {
  List<ChatListDataBean> dataList = [];

  ChatFriendController chatFriendController;
  int index = 0;
  final MainController mainController;
  ChatListController(this.mainController) : super(ChatListPage());

  @override
  void initState() async {
    super.initState();
    chatFriendController = ChatFriendController(this);
    _initListener();
    if(UserManager.instance.isLogin){

      WebSocketManager.instance.sendMold(WsBean.chatList);
    }
  }

  int lastId = -1;

  @override
  void onResume() async {
    super.onResume();
    if(index==1) {
      chatFriendController.onResume();
    }
    if (lastId == UserManager.instance.data.id && dataList.isNotEmpty) {
      return;
    }
    WebSocketManager.instance.sendMold(WsBean.chatList);
  }

  void _initListener() {
    WebSocketManager.instance.addListener(WsBean.chatList, (p0) {
      lastId = UserManager.instance.data.id;
      final json = const JsonDecoder().convert(p0.value);
      final result = ChatListBean(json);
      dataList.clear();
      dataList.addAll(result.data);
      setTips();
      setState();
    });
    WebSocketManager.instance.addListener(WsBean.chat, (p0) async {
      inserMessage(p0.id, p0.value, true);
      bool canVibrate = await Vibrate.canVibrate;
      if(canVibrate){
        var _type = FeedbackType.warning;
         Vibrate.feedback(_type);
        await Future.delayed(const Duration(milliseconds: 370));
        Vibrate.feedback(FeedbackType.heavy);
      }
    });
  }

  ///插入消息
  void inserMessage(int id, String message, bool num //是否自增消息提示数量
      ) {
    for (var value in dataList) {
      if (value.sendId == id) {
        value.lastMessage = message;
        var time = DateFormat("HH:mm").format(DateTime.now());
        if (DateTime.now().hour > 12) {
          time = "下午 " + time;
        } else {
          time = "上午 " + time;
        }
        value.lastTime = time;
        if (num) {
          value.num++;
        }
        dataList.remove(value);
        dataList.insert(0, value);
        setState();
        setTips();
        return;
      }
    }

    //如果新的聊天记录，刷新列表。
    WebSocketManager.instance.sendMold(WsBean.chatList);
  }

  @override
  void dispose() {
    super.dispose();
    WebSocketManager.instance.removeListener(WsBean.chatList);
    WebSocketManager.instance.removeListener(WsBean.chat);
  }

  onItemClick(ChatListDataBean item) {
    item.num = 0;
    ChatRoomController(this, item.id, item.receiveNick, item.receiveLogo)
        .push(context);
    setTips();
    setState();
  }

  void setTips(){
    int tips=0;
    for (var value in dataList) {
      tips+=value.num;
    }
    mainController.setTips(tips);
  }

  onScreenItemClick(int index) {
    if(this.index==index) return;
    this.index = index;
    if(index==1){
      chatFriendController.onResume();
    }
    setState();
  }
}
